package com.dkd.manage.controller;

import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.R;
import com.dkd.common.core.page.TableDataInfo;
import com.dkd.common.enums.BusinessType;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.manage.domain.TaskDetails;
import com.dkd.manage.service.ITaskDetailsService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/manage/taskDetails")
@Api(tags = "工单详情管理", description = "提供工单详情相关的操作接口")
public class TaskDetailsController extends BaseController {

    @Autowired
    private ITaskDetailsService taskDetailsService;

    /**
     * 查询工单详情列表
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:list')")
    @ApiOperation(value = "查询工单详情列表", notes = "根据条件查询工单详情列表")
    @GetMapping("/list")
    public R<TableDataInfo> list(TaskDetails taskDetails) {
        startPage();
        List<TaskDetails> list = taskDetailsService.selectTaskDetailsList(taskDetails);
        return R.ok(getDataTable(list));
    }

    /**
     * 导出工单详情列表
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:export')")
    @Log(title = "工单详情", businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出工单详情列表", notes = "根据条件导出工单详情到Excel文件")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功导出")
    })
    @PostMapping("/export")
    public R<String> export(HttpServletResponse response, TaskDetails taskDetails) {
        try {
            List<TaskDetails> list = taskDetailsService.selectTaskDetailsList(taskDetails);
            ExcelUtil<TaskDetails> util = new ExcelUtil<>(TaskDetails.class);
            util.exportExcel(response, list, "工单详情数据");
            return R.ok("导出成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("导出失败：" + e.getMessage());
        }
    }

    /**
     * 获取工单详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:query')")
    @ApiOperation(value = "获取工单详情详细信息", notes = "通过ID获取工单详情的信息")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功获取", response = TaskDetails.class)
    })
    @GetMapping(value = "/{detailsId}")
    public R<TaskDetails> getInfo(@ApiParam(value = "工单详情ID", required = true) @PathVariable("detailsId") Long detailsId) {
        TaskDetails details = taskDetailsService.selectTaskDetailsByDetailsId(detailsId);
        if (details != null) {
            return R.ok(details);
        } else {
            return R.fail("未找到对应的工单详情");
        }
    }

    /**
     * 新增工单详情
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:add')")
    @Log(title = "工单详情", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增工单详情", notes = "创建一个新的工单详情记录")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功插入", response = Integer.class)
    })
    @PostMapping
    public R<Integer> add(@RequestBody TaskDetails taskDetails) {
        int result = taskDetailsService.insertTaskDetails(taskDetails);
        if (result > 0) {
            return R.ok(result);
        } else {
            return R.fail("新增失败");
        }
    }

    /**
     * 修改工单详情
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:edit')")
    @Log(title = "工单详情", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改工单详情", notes = "更新指定ID的工单详情记录")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功更新", response = Integer.class)
    })
    @PutMapping
    public R<Integer> edit(@RequestBody TaskDetails taskDetails) {
        int result = taskDetailsService.updateTaskDetails(taskDetails);
        if (result > 0) {
            return R.ok(result);
        } else {
            return R.fail("修改失败");
        }
    }

    /**
     * 删除工单详情
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:remove')")
    @Log(title = "工单详情", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除工单详情", notes = "删除一个或多个工单详情记录")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功删除", response = Integer.class)
    })
    @DeleteMapping("/{detailsIds}")
    public R<Integer> remove(@ApiParam(value = "要删除的工单详情ID数组", required = true) @PathVariable Long[] detailsIds) {
        int result = taskDetailsService.deleteTaskDetailsByDetailsIds(detailsIds);
        if (result > 0) {
            return R.ok(result);
        } else {
            return R.fail("删除失败");
        }
    }

    /**
     * 查看工单补货详情
     */
    @PreAuthorize("@ss.hasPermi('manage:taskDetails:list')")
    @ApiOperation(value = "查看工单补货详情", notes = "根据任务ID查询工单补货详情")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功获取", response = TaskDetails.class)
    })
    @GetMapping("/byTaskId/{taskId}")
    public R<List<TaskDetails>> byTaskId(@ApiParam(value = "任务ID", required = true) @PathVariable("taskId") Long taskId) {
        TaskDetails taskDetails = new TaskDetails();
        taskDetails.setTaskId(taskId);
        List<TaskDetails> list = taskDetailsService.selectTaskDetailsList(taskDetails);
        return R.ok(list);
    }
}